package com.cn.yugan.Engine.Http;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.cn.yugan.Engine.Beans.NoticeMessageBeans;
import com.cn.yugan.Engine.Beans.NoticeNormalBean;
import com.cn.yugan.Engine.Json.NoticeMessageJsonObj;
import com.cn.yugan.Engine.Json.NoticeNormalMessageJsonObj;
import com.cn.yugan.Utils.GsonUtils;
import com.cn.yugan.Utils.LogUtils;
import com.cn.yugan.mobile.R;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;

import java.io.UnsupportedEncodingException;

/**
 * Created by Rover on 2017/1/6 .
 * Email 357319517@qq.com
 */
public class Nocation {

    /**
     * 获取消息个数
     */
    final public static int NOTICE_ALL = 0;
    /**
     * 消息获取的异步循环
     */
    final public static int NOTICE_LOOPER = -1;
    /**
     * 获取ATME的信息
     */
    final public static int NOTICE_ATME = 1;
    /**
     * 获取回复我的信息
     */
    final public static int NOTICE_REPLYME = 2;
    /**
     * 获取私信的信息
     */
    final public static int NOTICE_MESSAGE = 3;
    /**
     * 从服务器获取notice
     *
     * @param type NOTICE_ALL:获取未响应个数
     *             NOTICE_ATME:被召唤的信息
     *             NOTICE_REPLYME：回复被引用的信息
     *             NOTICE_MESSAGE：私信的信息
     */
    public static void getnotice(Context context, final int type, AsyncHttpClient asyncHttpClient, final Handler mhandler) {
        String s = "all";
        switch (type) {
            case NOTICE_ALL:
                s = "all";
                break;
            case NOTICE_ATME:
                s = "atme";
                break;
            case NOTICE_REPLYME:
                s = "replyme";
                break;
            case NOTICE_MESSAGE:
                s = "message";
                break;
        }
        RequestParams params = new RequestParams();
        params.put("type", s);
        LogUtils.e(s);
        asyncHttpClient.post(context.getString(R.string.serverurl) + context.getString(R.string.getnotice), params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                try {
                    String s = new String(bytes, "utf-8");
                    if (type==NOTICE_MESSAGE) {
                        NoticeMessageJsonObj json = GsonUtils.GsonToBean(s, NoticeMessageJsonObj.class);
                        Message msg = Message.obtain();
                        msg.obj = json;
                        msg.what = type;
                        LogUtils.e("message");
                        mhandler.sendMessage(msg);
                    }else {
                        NoticeNormalMessageJsonObj json = GsonUtils.GsonToBean(s, NoticeNormalMessageJsonObj.class);
                        Message msg = Message.obtain();
                        msg.obj = json;
                        msg.what = type;
                        LogUtils.e("normal");
                        mhandler.sendMessage(msg);
                    }
                } catch (UnsupportedEncodingException e) {
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
            }
        });
    }

    /**
     * 清除notice
     *
     * @param type NOTICE_ALL:所有信息
     *             NOTICE_ATME:被召唤的信息
     *             NOTICE_REPLYME：回复被引用的信息
     *             NOTICE_MESSAGE：私信的信息
     */
    public static void clearnotice(Context context, final int type,AsyncHttpClient asyncHttpClient){
        String s = "all";
        switch (type) {
            case NOTICE_ALL:
                s = "all";
                break;
            case NOTICE_ATME:
                s = "atme";
                break;
            case NOTICE_REPLYME:
                s = "replyme";
                break;
            case NOTICE_MESSAGE:
                s = "message";
                break;
        }
        RequestParams params = new RequestParams();
        params.put("type", s);
        asyncHttpClient.post(context.getString(R.string.serverurl) + context.getString(R.string.clearnotice), params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
            }
        });
    }
}
